package com.bwie.agent.mq;

import com.bwie.agent.config.MsgUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName Consumer
 * @Description TODO
 * @Author 李奉武
 * @Motto 逆水行舟, 不进则退
 * @Date 2022/3/25 10:34
 * @Version 1.0
 */
@Component
@RabbitListener(queues = "06a")
public class Consumer {


    @Autowired
    StringRedisTemplate stringRedisTemplate;


    /**
     * mq异步发短信 basicack机制保障消息消费者端消息不丢失
     * */
    @RabbitHandler
    public void sendmsg(Message message, String tel, Channel channel) throws IOException {
        String s = MsgUtil.sendMsg(tel);
        stringRedisTemplate.opsForValue().set(tel, s);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
